import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/stores'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    { path: '/login', component: () => import('@/views/Login') }
    // {
    //   path: '/',
    //   redirect: '/home',
    //   component: () => import('@/views/Layout/index.vue'),
    //   children: [
    //     {
    //       path: '/home',
    //       component: () => import('@/views/Home/index.vue'),
    //       meta: { title: '首页' }
    //     },
    //   ]
    // }
  ]
})

/**
 * 访问权限控制
 */
const wihteList: Array<string> = ['/login', '/register']

router.beforeEach(() => {
  NProgress.start()
})

router.beforeEach((to) => {
  const userStore = useUserStore()
  if (!userStore.user?.token && !wihteList.includes(to.path)) {
    return { path: '/login' }
  }
})

/**
 * 页面标题
 */
router.afterEach((to) => {
  document.title = `${(to.meta.title as string) || ''}-优医问诊`
})
router.afterEach(() => {
  NProgress.done()
})

/**
 * 修改颜色配置，去掉右上角的圆圈
 */
NProgress.configure({
  showSpinner: false
})
export default router
